/*
 * All rights Reserved, Designed By cloudland Copyright(C) 2010-2014
 * 
 * fileName:  Calculates.java
 * author:  lei
 * Company:  Cloudland Studio
 * Createdate:  2013-9-14 上午9:30:33
 *
 * Modification  History:
 *     Date           Author        Version        
 * -------------------------------------------
 * 2013-9-14          Lei            1.0        
 *
 * Why & What is modified:
 * 1.0: 文件创建
 */
package org.cloudland.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

 /**
 * <pre> 
 * 计算工具类
 * </pre>
 * @ClassName  Calculates
 * @author  lei
 * @version  %I%, %G%
 * @see  
 * @since   JDK1.6
 *
 */
public class Calculates {
	
	/**
	 * <pre>
	 * 私有化构造函数，此类不允许实例化。
	 * </pre>
	 *   
	 */
	private Calculates(){}
	
	/**
	 * <pre>
	 * 两个整形求和
	 * </pre>
	 *
	 * @param num1  第一个整数
	 * @param num2  第二个整数
	 * @return   
	 */
	public static Integer plus(Integer num1, Integer num2)
    {
        Integer sum = null;
        if (num1 != null && num2 != null)
        {
            sum = num1 + num2;
        }
        return sum;
    }
	
	/**
	 * <pre>
	 * 两个浮点数求和
	 * </pre>
	 *
	 * @param num1
	 * @param num2
	 * @return   
	 */
	public static Float plus(Float num1, Float num2)
    {
        Float sum = null;
        if (num1 != null && num2 != null)
        {
            BigDecimal bb = new BigDecimal(num1).add(new BigDecimal(num2));
            sum = bb.floatValue();
        }
        return sum;
    }

	/**
	 * <pre>
	 * 两个double类型的求和
	 * </pre>
	 *
	 * @param num1
	 * @param num2
	 * @return   
	 */
	public static Double plus(Double num1, Double num2)
    {
        Double sum = null;
        
        if (num1 != null && num2 != null)
        {
            BigDecimal bb = new BigDecimal(num1).add(new BigDecimal(num2));
            sum = bb.doubleValue();
        }
        return sum;
    }
	
	/**
	 * <pre>
	 * 两个String类型的求和
	 * </pre>
	 *
	 * @param num1
	 * @param num2
	 * @return   
	 */
	public static String plus(String num1, String num2)
    {
        String sum = null;
        if (num1 != null && num2 != null)
        {
            if (!isNumber(num1) || !isNumber(num2))
            {
                num1 = "0";
                num2 = "0";
            }
            Float floatSum = plus(Float.parseFloat(num1),
                    Float.parseFloat(num2));
            sum = String.valueOf(floatSum);
        }
        return sum;
    }
	
	/**
	 * <pre>
	 * 两个整数相减
	 * </pre>
	 *
	 * @param num1
	 * @param num2
	 * @return   
	 */
	public static Integer minus(Integer num1, Integer num2)
    {
        Integer value = null;
        if (num1 != null && num2 != null)
        {
            value = num1 - num2;
        }
        return value;
    }
	
	/**
	 * <pre>
	 * 两个浮点数求差
	 * </pre>
	 *
	 * @param num1
	 * @param num2
	 * @return   
	 */
	public static Float minus(Float num1, Float num2)
    {
        Float value = null;
        
        if (num1 != null && num2 != null)
        {
            BigDecimal bb = new BigDecimal(num1).subtract(new BigDecimal(num2));
            value = bb.floatValue();
        }
        return value;
    }
	
	/**
	 * <pre>
	 * 两个浮点数求差
	 * </pre>
	 *
	 * @param num1
	 * @param num2
	 * @return   
	 */
	public static Double minus(Double num1, Double num2)
    {
        Double value = null;
        
        if (num1 != null && num2 != null)
        {
            BigDecimal bb = new BigDecimal(num1).subtract(new BigDecimal(num2));
            value = bb.doubleValue();
        }
        return value;
    }
	
	/**
	 * <pre>
	 * 两个String类型的求差
	 * </pre>
	 *
	 * @param num1
	 * @param num2
	 * @return   
	 */
	public static String minus(String num1, String num2)
    {
        String sum = null;
        if (num1 != null && num2 != null)
        {
            if (!isNumber(num1) || !isNumber(num2))
            {
                num1 = "0";
                num2 = "0";
            }
            Float floatSum = minus(Float.parseFloat(num1),
                    Float.parseFloat(num2));
            sum = String.valueOf(floatSum);
        }
        return sum;
    }

	/**
	 * <pre>
	 * 两个整数求乘积
	 * </pre>
	 *
	 * @param num1
	 * @param num2
	 * @return   
	 */
	public static Integer multiply(Integer num1, Integer num2)
    {
        Integer value = null;
        if (num1 != null && num2 != null)
        {
            value = num1 * num2;
        }
        return value;
    }
	
	/**
	 * <pre>
	 * 两个Float求乘积
	 * </pre>
	 *
	 * @param num1
	 * @param num2
	 * @return   
	 */
	public static Float multiply(Float num1, Float num2)
    {
        Float value = null;
        
        if (num1 != null && num2 != null)
        {
            BigDecimal bb = new BigDecimal(num1).multiply(new BigDecimal(num2));
            value = bb.floatValue();
        }
        return value;
    }
	
	/**
	 * <pre>
	 * 两个Double求乘积
	 * </pre>
	 *
	 * @param num1
	 * @param num2
	 * @return   
	 */
	public static Double multiply(Double num1, Double num2)
    {
        Double value = null;
        
        if (num1 != null && num2 != null)
        {
            BigDecimal bb = new BigDecimal(num1).multiply(new BigDecimal(num2));
            value = bb.doubleValue();
        }
        return value;
    }
	
	/**
	 * <pre>
	 * 两个String求乘积
	 * </pre>
	 *
	 * @param num1
	 * @param num2
	 * @return   
	 */
	public static String multiply(String num1, String num2)
    {
        String value = null;
        if (num1 != null && num2 != null)
        {
            if (!isNumber(num1) || !isNumber(num2))
            {
                num1 = "0";
                num2 = "0";
            }
            Float floatSum = multiply(Float.parseFloat(num1),
                    Float.parseFloat(num2));
            value = String.valueOf(floatSum);
        }
        return value;
    }
	
	/**
	 * <pre>
	 * 两个整数相除
	 * </pre>
	 *
	 * @param num1
	 * @param num2
	 * @return   
	 */
	public static Integer divide(Integer num1, Integer num2)
    {
        Integer value = null;
        if (num1 != null && num2 != null)
        {
            BigDecimal bb = new BigDecimal(num1).divide(new BigDecimal(num2));
            value = bb.intValue();
        }
        return value;
    }
	
	/**
	 * <pre>
	 * 两个Float相除
	 * </pre>
	 *
	 * @param num1
	 * @param num2
	 * @return   
	 */
	public static Float divide(Float num1, Float num2)
    {
        Float value = null;
        
        if (num1 != null && num2 != null)
        {
            BigDecimal bb = new BigDecimal(num1).divide(new BigDecimal(num2), 3);
            value = bb.floatValue();
        }
        return value;
    }
	
	/**
	 * <pre>
	 * 两个Double相除
	 * </pre>
	 *
	 * @param num1
	 * @param num2
	 * @return   
	 */
	public static Double divide(Double num1, Double num2)
    {
        Double value = null;
        
        if (num1 != null && num2 != null)
        {
            BigDecimal bb = new BigDecimal(num1).divide(new BigDecimal(num2), 3);
            value = bb.doubleValue();
        }
        return value;
    }
	
	/**
	 * <pre>
	 * 两个String相除
	 * </pre>
	 *
	 * @param num1
	 * @param num2
	 * @return   
	 */
	public static String divide(String num1, String num2)
    {
        String value = null;
        if (num1 != null && num2 != null)
        {
            if (!isNumber(num1) || !isNumber(num2))
            {
                num1 = "0";
                num2 = "1";
            }
            Float floatSum = divide(Float.parseFloat(num1),
                    Float.parseFloat(num2));
            value = String.valueOf(floatSum);
        }
        return value;
    }
	
	/**
	 * <pre>
	 * 格式化对象
	 * </pre>
	 *
	 * @param num
	 * @return   
	 */
	public static String format(Object num)
    {
        if (num instanceof String)
        {
            try
            {
                num = Float.parseFloat((String) num);
            }
            catch (NumberFormatException e)
            {
                throw new NumberFormatException();
            }
        }
        DecimalFormat format = new DecimalFormat("#,###.00");
        return format.format(num);
    }
	
	/**
	 * <pre>
	 * 判断传入的字符是否是数字
	 * </pre>
	 *
	 * @param str
	 * @return   
	 */
	private static boolean isNumber(String str)
    {
        Pattern pattern = Pattern.compile("(-)?[0-9]+(.[0-9]+)?");
        Matcher isNum = pattern.matcher(str);
        return isNum.matches();
    }
	
	 /**
	 * <pre> 
	 * 数字转换中文
	 * </pre>
	 * @ClassName  CnUpperCaser
	 * @author  lei
	 * @version  %I%, %G%
	 * @see  
	 * @since   JDK1.6
	 *
	 */
	private static class CnUpperCaser
    {
        // 整数部分
        private String integerPart;
        
        // 小数部分
        private String floatPart;
        
        // 将数字转化为汉字的数组,因为各个实例都要使用所以设为静态
        private static final char[] cnNumbers = {'零', '壹', '贰', '叁', '肆', '伍',
                '陆', '柒', '捌', '玖'};
        
        // 供分级转化的数组,因为各个实例都要使用所以设为静态
        private static final char[] series = {'元', '拾', '百', '仟', '万', '拾',
                '百', '仟', '亿', '拾', '百', '仟'};
        
        /**
         * <pre>
         * 构造函数,通过它将阿拉伯数字形式的字符串传入
         * </pre>
         *
         * @param original   
         */
        public CnUpperCaser(String original)
        {
            // 成员变量初始化
            integerPart = "";
            floatPart = "";
            
            if (original.contains("."))
            {
                // 如果包含小数点
                int dotIndex = original.indexOf(".");
                integerPart = original.substring(0, dotIndex);
                floatPart = original.substring(dotIndex + 1);
            }
            else
            {
                // 不包含小数点
                integerPart = original;
            }
        }
        
        /**
         * <pre>
         * 取得大写形式的字符串
         * </pre>
         *
         * @return   
         */
        public String getCnString()
        {
            // 因为是累加所以用StringBuffer
            StringBuffer sb = new StringBuffer();
            
            // 整数部分处理
            for (int i = 0; i < integerPart.length(); i++)
            {
                int number = getNumber(integerPart.charAt(i));
                
                sb.append(cnNumbers[number]);
                sb.append(series[integerPart.length() - 1 - i]);
            }
            
            // 小数部分处理
            if (floatPart.length() > 0)
            {
                sb.append("点");
                for (int i = 0; i < floatPart.length(); i++)
                {
                    int number = getNumber(floatPart.charAt(i));
                    
                    sb.append(cnNumbers[number]);
                }
            }
            
            // 返回拼接好的字符串
            return sb.toString();
        }
        
        /**
         * <pre>
         * 将字符形式的数字转化为整形数字 因为所有实例都要用到所以用静态修饰
         * </pre>
         *
         * @param c
         * @return   
         */
        private static int getNumber(char c)
        {
            String str = String.valueOf(c);
            return Integer.parseInt(str);
        }
        
    }
    
    /**
     * <pre>
     * 将数字转换为中文
     * </pre>
     *
     * @param num
     * @return   
     */
    public static String formatToChina(Object num)
    {
        String original = null;
        if (num instanceof String)
        {
            original = (String) num;
            if (!isNumber(original))
            {
                //TODO 传入的字符串不能转换数字
            }
        }
        else
        {
            original = num.toString();
        }
        CnUpperCaser cn = new CnUpperCaser(original);
        return cn.getCnString();
    }
}
